/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.vod.gauss.matrice;

import fr.vod.gauss.utils.Convert;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author drazanakoto
 */
public class MatriceImpl implements Matrice {

    private List<Equation> equations;

    /**
     *
     * @param mat
     * @param results
     */
    public MatriceImpl(int[][] mat, int[] results) {
        equations = new ArrayList<Equation>();
        if (mat != null && results != null) {
            for (int i = 0; i < mat.length; i++) {
                equations.add(new EquationImpl(mat[i], results[i]));
            }
        }
    }

    /**
     *
     */
    public MatriceImpl() {
        this(null, null);
    }

    /**
     *
     * @return
     */
    @Override
    public int getLength() {
        return equations.size();
    }

    /**
     *
     * @param index
     * @return
     */
    @Override
    public Equation get(int index) {
        return equations.get(index);
    }

    /**
     *
     * @param index
     */
    @Override
    public void remove(int index) {
        equations.remove(index);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Equation e : equations) {
            sb.append(e.toString()).append("\n");
        }
        return sb.toString();
    }

    @Override
    public void move(int src, int dest) {
        if (dest < equations.size() && dest >= 0 && src < equations.size() && src >= 0) {
            Equation tmp = equations.get(src);
            equations.remove(src);
            equations.add(dest, tmp);
        }
    }

    @Override
    public void addEquation(Equation e) {
        equations.add(e);
    }

    @Override
    public byte[] rebuildFile() {
        byte file[] = new byte[equations.size()];
        int i = 0;
        for (Equation e : equations) {
            file[i] = Convert.uint8ToByte(e.getResult());
            i++;
        }
        return file;
    }
}
